package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    MemberService memberService;

    @RequestMapping("/getMemberAllCount")
    public Result getMemberAllCount() {
        try {
            List<Map<String, Object>> memberAllCount = memberService.getMemberAllCount();
            List<Object> memberSexs = new ArrayList<>();
            for (Map<String, Object> map : memberAllCount) {
                Object name = map.get("name");
                memberSexs.add(name);
            }
            HashMap<String, Object> resultMap = new HashMap<>();
            resultMap.put("memberSexs", memberSexs);
            resultMap.put("memberAllCount", memberAllCount);
            System.out.println("resultMap"+resultMap.toString());
            return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.GET_MEMBER_NUMBER_REPORT_FAIL);
        }

    }
    @RequestMapping("/getMemberAgeCount")
    public Result getMemberAgeCount() {
        try {
            List<Map<String, Object>> memberAgeCount = memberService.getMemberAgeCount();
            List<Object> memberAges = new ArrayList<>();
            for (Map<String, Object> map : memberAgeCount) {
                Object name = map.get("name");
                memberAges.add(name);
            }
            HashMap<String, Object> resultMap1 = new HashMap<>();
            resultMap1.put("memberAges", memberAges);
            resultMap1.put("memberAgeCount", memberAgeCount);
            System.out.println(resultMap1.toString());
            return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, resultMap1);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }



    //根据时间段查询人数
    //默认显示
    @RequestMapping("findByDate")
    public Result findByDate(){
        try{
            Map<String, Object> byDate = memberService.findByDate();
            return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,byDate);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConst.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }


    //根据时间段查询人数
    //自定义显示
    @RequestMapping("/findByDateZdy")
    public Result findByDateZdy(@RequestBody Map<String,String> map ){
        try{
            Map<String, Object> byDate = memberService.findByDateZdy(map);
            return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,byDate);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConst.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }





}
